﻿using System;
using System.Collections;
using System.Collections.Specialized;
using System.IO;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading;
using Program;

namespace Program
{
    class Program
    {
        static void Main()
        {
            HttpListener listener = new HttpListener();
            listener.Prefixes.Add("http://*:8000/");
            listener.Start();
            Console.WriteLine("HttpListener port 8000...");
            for (; ; )
            {
                try
                {
                    HttpListenerContext ctx = listener.GetContext();
                    new Thread(new http_server(ctx).ProcessRequest).Start();
                }
                catch (Exception e) 
                {
                    Console.WriteLine("EXCEPTION: " + e.Message);
                }
            }
        }

        class http_server
        {
            private HttpListenerContext context;

            public http_server(HttpListenerContext context)
            {
                this.context = context;
            }

            public void ProcessRequest()
            {
//              string msg = context.Request.HttpMethod + " " + context.Request.Url;
//              Console.WriteLine(msg);
//              string[] url = msg.Split('/');                
//              string[] request = url[url.Length - 1].Split(':');                
                HttpListenerRequest reqw = context.Request;                 //

                StreamReader reader = new StreamReader(reqw.InputStream);   //
                string s2 = reader.ReadToEnd();                             //
                Console.WriteLine(s2);                                      //

                string answer = null;
                string[] request = s2.Split(';');                           //

//delete 7 rows
                //string msg =context.Request.HttpMethod + " " + context.Request.Url;
                //string[] url = (msg).Split('/');                
                //string[] request111 = url[url.Length - 1].Split(':');
                //if (request111[0] == "SENDFILE" || request111[0] == "CREATE" || request111[0] == "READ" || request111[0] == "UPDATE" || request111[0] == "DELETE" || request111[0] == "CLEAR")
                //{
                //    request = request111;
                //}
//delete 7 rows

                switch (request[0])
                {
                    case "SENDFILE":
                        {
                            string fileName = request[1];
                            
                            answer = DS_CSV_server.SaveFile( fileName, new byte[1]);
                            break;
                        }
                    case "CREATE":
                        {
                            string person = "";
                            for (int i = 1; i < request.Length; i++)
                            {
                                person += request[i];
                                if (i < request.Length - 1) { person += ";"; }
                            }
                            answer = DS_CSV_server.Create(person);
                            break;
                        }
                    case "READ":
                        {
                            answer = DS_CSV_server.Read();
                            break;
                        }
                    case "UPDATE":
                        {
                            string person = "";
                            for (int i = 2; i < request.Length; i++)
                            {
                                person += request[i];
                                if (i < request.Length - 1) { person += ";"; }
                            }
                            answer = DS_CSV_server.Update(Convert.ToInt32(request[1]), person);
                            break;
                        }
                    case "DELETE":
                        {
                            answer = DS_CSV_server.Delete(Convert.ToInt32(request[1]));
                            break;
                        }
                    case "CLEAR":
                        {
                            answer = DS_CSV_server.Clear();
                            break;
                        }
                    default:
                        {
                            Console.WriteLine("отправлена веб страничка");
                            answer = File.ReadAllText("HTMLPage1.htm");

                            break;
                        }
                }

                byte[] b = Encoding.UTF8.GetBytes(answer);
                context.Response.ContentLength64 = b.Length;
                context.Response.OutputStream.Write(b, 0, b.Length);
                context.Response.OutputStream.Close();
            }

        }
    }
}